<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>结算明细</span>
          </div>
        </div>
        <div class="mainor_form batch-wrap form-item-mg mg-t-14">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
          >
            <el-form-item label="单号：" prop="order_sn">
              <el-input
                v-model.trim="form.order_sn"
                placeholder="单号"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item label="用户：" prop="uid">
              <el-input
                v-model.trim="form.uid"
                placeholder="用户"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>

            <el-form-item label="结算日期：" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getArticleList" :loading="listLoading" :showBtn="moredata">
              <el-button @click="export_de" type="primary"
                >导出<el-tooltip class="item" effect="dark" placement="bottom">
                  <div slot="content">
                    注释：默认导出全部，也可勾选导出你想要的数据
                  </div>
                  <svg
                    aria-hidden="true"
                    class="navbar_svg new_navbar_svg"
                    width="14"
                    height="14"
                  >
                    <use xlink:href="#icon-summary_question" />
                  </svg> </el-tooltip
              ></el-button>
            </MainFormBtn>
          </el-form>
        </div>

        <div class="main_table" style="min-height: 450px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection"></el-table-column>
            <el-table-column
              label="原始单号"
              width="200"
              prop="related_order_order_sn"
            />
            <el-table-column label="单据类型" width="120" prop="status">
              <template slot-scope="scope">
                <span :class="'order' + scope.row.type">{{
                  list.typeList[scope.row.type]
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="账单号"
              width="200"
              prop="related_order_sn"
            />
            <el-table-column
              label="本次收款金额"
              width="120"
              prop="collect_money"
            />
            <el-table-column label="抹零金额" width="120" prop="status">
              <template slot-scope="scope">{{ scope.row.zero_money }}</template>
            </el-table-column>
            <el-table-column label="剩余未收" prop="out_money" />
            <el-table-column label="收款方式">
              <template slot-scope="scope">
                <span
                  :class="
                    scope.row.order_offline_settlement
                      ? 'pay_type' + scope.row.order_offline_settlement.pay_type
                      : ''
                  "
                >
                  {{
                    scope.row.order_offline_settlement
                      ? list.pay_type_list[
                          scope.row.order_offline_settlement.pay_type
                        ]
                      : ""
                  }}
                </span>
              </template>
            </el-table-column>
            <el-table-column label="结算单号" prop="">
              <template slot-scope="scope">
                {{
                  scope.row.order_offline_settlement
                    ? scope.row.order_offline_settlement.order_sn
                    : ""
                }}
              </template></el-table-column
            >
            <el-table-column label="结算日期" prop="">
              <template slot-scope="scope">{{
                scope.row.order_offline_settlement
                  ? parseTime(
                      scope.row.order_offline_settlement.created_at,
                      "{y}-{m}-{d}"
                    )
                  : ""
              }}</template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 导出 -->
    <el-dialog
      title="导出"
      :visible.sync="exportbuVisible"
      class="main_dialog dialog500"
    >
      <div v-if="selectArr.length == 0 ? true : false">
        <p class="exptitle">创建日期</p>
        <el-date-picker
          v-model="exportTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          @change="datepicker"
        >
        </el-date-picker>
        <!-- <el-tooltip
          class="item"
          effect="dark"
          content="时间范围只能选择一个月之内"
          placement="bottom"
        >
          <i class="el-icon-question"></i>
        </el-tooltip> -->
      </div>
      <div v-if="selectArr.length == 0 ? true : false">
        <p class="exptitle company_name">采购方式</p>
        <el-select v-model.trim="mode" placeholder="采购方式" class="wid220">
          <el-option label="所有" value=""></el-option>
          <el-option
            v-for="(modeList, index) in list.modeList"
            :key="index"
            :label="modeList"
            :value="parseInt(index)"
          ></el-option>
        </el-select>
      </div>
      <div v-if="selectArr.length == 0 ? false : true" class="maintip">
        你已经选择了<span>{{ selectArr.length }}</span
        >行数据
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="exportbuVisible = false">取 消</el-button>
        <el-button type="primary" @click="exportConfirm" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  offlineSettle_getList,
  export_offlineSettle_getList,
} from "@/api/financial/finacialCashOnDelivery";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { parseTime, exportMethod } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 时间
      parseTime,
      exportMethod,
      // 列表数据
      list: {
        list: {},
        pay_type_list: [],
        typeList: [],
        pay_money_all: "",
      },

      listLoading: false,
      // 搜索
      form: {
        order_sn: "",
        start_time: "",
        end_time: "",
        uid: "",
      },
      createTime: [],
      pickerOptions: dateOption,

      // 导出
      selectArr: [],
      exportbuVisible: false,
      exportTime: "",
      mode: "",
    };
  },
  created() {
    // 列表
    this.offlineSettle_getList();
  },
  methods: {
    reset() {
      this.$refs['userListFrom'].resetFields()
      this.offlineSettle_getList();
    },
    // 导出明细

    exportConfirm() {
      let ids = "";
      if (this.selectArr.length > 0) {
        ids = this.selectArr.join("-");
      }

      let form = {
        ids: ids,
        start_time: "",
        end_time: "",
        mode: this.mode,
        order_sn: "",
      };
      if (this.exportTime) {
        form.start_time = this.exportTime[0];
        form.end_time = this.exportTime[1];
        delete form.ids;
      }
      if (!form.ids && !form.start_time) {
        this.$message.error("请选择导出的订单或者导出的时间");
        return;
      }
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_advice_detail(form)
        .then((res) => {
          this.exportMethod(res, "采购结算列表明细表", notify);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
      this.exportbuVisible = false;
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.offlineSettle_getList();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.offlineSettle_getList();
    },
    changePage(page) {
      this.current.page = page;
      this.offlineSettle_getList();
    },
    // 列表
    offlineSettle_getList() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      offlineSettle_getList(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";
      return form;
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push(item.id);
      });
    },
    // 导出时间范围
    datepicker(val) {
      let start = "";
      let end = "";
      if (val) {
        start = new Date(val[0]).getTime();
        end = new Date(val[1]).getTime();
        if (end - start > 30 * 24 * 60 * 60 * 1000) {
          this.$message.error("选择范围不能超过一个月，请重新选择");
          this.exportTime = "";
        }
      }
    },
    // 列表导出
    export_de() {
      let form = {};
      if (this.selectArr.length > 0) {
        form.ids = this.selectArr.join(",");
      } else {
        let idarr = [];
        this.list.list.data.forEach((item) => {
          idarr.push(item.id);
        });
        form.ids = idarr.join(",");
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_offlineSettle_getList(data)
        .then((res) => {
          this.exportMethod(res, "货到付款结算单-结算明细", notify);
        })
        .catch((err) => {
          this.$message.error("导出失败");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 220px);
  }
}

// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
}
.company_name {
  padding-top: 15px;
}
.maintip {
  text-align: center;
  padding-top: 15px;
  font-size: 18px;
  span {
    display: inline;
    padding: 18px 0;
    color: red;
  }
}
.isexportCheckbox {
  margin-top: 15px;
}
.company_name_box {
  .company_name {
    padding-top: 0;
  }
}
.pay_type1 {
  color: #118ab2;
}
.pay_type2 {
  color: #03a98e;
}
.pay_type3 {
  color: #aacd6e;
}
.pay_type4 {
  color: #073b4c;
}
.order1 {
  color: #03a98e;
}
.order2 {
  color: #f56c6c;
}
.new_navbar_svg {
  vertical-align: top;
  margin-left: 5px;
  use {
    fill: #ffffff;
  }
}
</style>
